package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.CheckGroup;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: CheckGroupController
 * @Author: Solomon
 * @Date: 2020/7/1 16:20
 * @Description:
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //分页条件查询检查组
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.findByPage(queryPageBean);
    }

    //添加检查组
    @RequestMapping("/add")
    public Result add(Integer[] checkItemIds, @RequestBody CheckGroup checkGroup){
        try {
            //直接调用添加方法，无异常则正确返回
            checkGroupService.add(checkItemIds,checkGroup);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            //有异常则返回错误信息
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    //根据id查询检查组基本信息
    @RequestMapping("/findById")
    public Result findById(Integer checkGroupId){
        try {
            CheckGroup checkGroup = checkGroupService.findById(checkGroupId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //根据检查组的id中间表中查询关联的检查项的id
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkGroupId){
        try {
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(checkGroupId);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //修改检查组的基本信息和中间表的关联信息
    @RequestMapping("/update")
    public Result update(Integer[] checkItemIds, @RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.update(checkItemIds,checkGroup);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //根据id删除检查组
    @RequestMapping("/deleteById")
    public Result deleteById(Integer checkGroupId){
        //先查询当前检查组是否关联的检查套餐
        int count = checkGroupService.findSetmealByCheckgroupIds(checkGroupId);


        if (count > 0){
            //说明当前检查组关联了检查套餐
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

        try {
            checkGroupService.deleteById(checkGroupId);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    //新增或编辑检查套餐时，查询所有的检查组
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
